package com.smartwebx.ha.web.admin.module.screen.user;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.dataresolver.Param;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.assign.HaUserManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.assign.bean.HaUser;

public class AjaxAddUser{
	@Autowired
	private HaUserManager haUserManager;
	@Autowired
	private HaAreaManager haAreaManager;
	public void execute(@Param("data") String dateJson, @Param("areaId") String areaId) throws IOException{
		if(areaId!=null&&!"".equals(areaId)){
			HaArea haArea = haAreaManager.findOne(Integer.parseInt(areaId));
			String code = haArea.getCode();
			dateJson = dateJson.substring(0, dateJson.length()-1) + ",\"code\":\"" + code + "\"}";
		}
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
				false);
		HaUser user=mapper.readValue(dateJson,
				HaUser.class);
		
		if(user.getId()==null){
			haUserManager.addUser(user);
		}else{
			haUserManager.updateUser(user);
		}
	}

}
